//
//  LLPersonMainView.m
//  UserUse
//
//  Created by Yiwi on 16/10/14.
//  Copyright © 2016年 CJPC. All rights reserved.
//

#import "LLPersonMainView.h"
#import "LLPersonTopView.h"

static NSString *reuseIdentifierCell = @"reuseIdentifierCell";

@interface LLPersonMainView ()<UITableViewDelegate,UITableViewDataSource,LLPersonTopViewDelegate>{
    LLPersonTopView * _topView;
    UITableView * _iTabelView;
    NSArray * _iconArray;
    NSArray * _titleArray;
    UIButton * _bt;
}

@end

@implementation LLPersonMainView
-(instancetype)init{
    if (self = [super init]) {
        self.backgroundColor = kColoeString(@"f4f4f4");
        _iconArray = @[@"page",@"message",@"about"];
        _titleArray = @[@"我的派单",@"系统消息",@"关于我们"];
    }
    return self;
}

-(void)setupSubView{
    [self setupTopView];
    [self setupTableVeiw];
    [self setupButton];
}

-(void)setupTopView{
    _topView = [LLPersonTopView new];
    _topView.backgroundColor = [UIColor whiteColor];
    _topView.delegate = self;

    [self addSubview:_topView];
    
    [_topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.height.equalTo(@(kScaleNum(85)));
    }];

}

-(void)setupTableVeiw{
    _iTabelView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    _iTabelView.delegate = self;
    _iTabelView.dataSource = self;
    _iTabelView.scrollEnabled = NO;
    [_iTabelView registerClass:[UITableViewCell class] forCellReuseIdentifier:reuseIdentifierCell];
    [self addSubview:_iTabelView];
    [_iTabelView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_topView.mas_bottom).offset(kScaleNum(10));
        make.left.right.equalTo(self);
        make.height.equalTo(@(kScaleNum(102)));
    }];
    if ([_iTabelView respondsToSelector:@selector(setSeparatorInset:)]) {
        [_iTabelView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }
    
    if ([_iTabelView respondsToSelector:@selector(setLayoutMargins:)]) {
        [_iTabelView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
}

-(void)setupButton{
    _bt = [UIButton new];
    _bt.backgroundColor = kColoeString(@"#ff9a02");
    _bt.layer.masksToBounds = YES;
    _bt.layer.cornerRadius = kScaleNum(4);
    [_bt setTitle:@"退出登录" forState:UIControlStateNormal];
    [_bt addTarget:self action:@selector(exitLogin) forControlEvents:UIControlEventTouchUpInside];
    _bt.titleLabel.textColor = kColoeString(@"ffffff");
    [self addSubview:_bt];
    [_bt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.top.equalTo(_iTabelView.mas_bottom).offset(kScaleNum(25));
        make.width.equalTo(@(kScaleNum(260)));
        make.height.equalTo(@(kScaleNum(40)));
    }];
}
-(void)updateTopViewWithUser:(LLUserModel*)user{
    [_topView clearView];
    user.UserTelephone ? [_topView setupNoLoginVeiw]:[_topView setupLoginView];
    [_topView setupLoginVeiwWithUer:self.user];
    _bt.hidden = ! user.UserTelephone;
}

-(void)exitLogin{
    [self.delegate exitLogin];
}
#pragma mark - tableView协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kScaleNum(34);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifierCell];
    cell.selectionStyle= UITableViewCellSelectionStyleNone;
    cell.imageView.image = [UIImage imageNamed:_iconArray[indexPath.row]];
    cell.textLabel.text = _titleArray[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.delegate didSelectRowAtIndexPath:indexPath];
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}
#pragma mark -LLPersonTopViewDelegate

-(void)login{
    [self.delegate login];
}

-(void)jumpdetail{
    [self.delegate jumpDetail];
}
@end
